import {request,RequestConfig,HttpRequestMethod,LengyuHttpResponse,download,upload, PageRequestParam}  from '../utils/request'

// 导出一个函数
export function doAuth(code:string): Promise<LengyuHttpResponse> {
  console.log("第三方平台认证");
  let requestConfig:RequestConfig={
    url:'/login/auth?code='+code,
    method:HttpRequestMethod.POST,
  }
  return request(requestConfig);
}


// 将微信用户信息保存到数据库
export function saveVxUser(nickName:string,avatarUrl:string): Promise<LengyuHttpResponse> {
  let data:any={
    vxOpenId:wx.getStorageSync('openId'),
    vxNickName:nickName,
    vxAvatar:avatarUrl
  }

  let requestConfig:RequestConfig={
    url:'/user/saveVxUser',
    method:HttpRequestMethod.POST,
    data:data,
    token:true,
  }
  return request(requestConfig);
}
export function pageUser(param:PageRequestParam): Promise<LengyuHttpResponse> {
  let requestConfig:RequestConfig={
    url:'user/page',
    method:HttpRequestMethod.POST,
    data:param,
    token:true,
  }
  return request(requestConfig);
}
export function listUser(vxNickName:string): Promise<LengyuHttpResponse> {
  let param={
    vxNickName:vxNickName
  }
  let requestConfig:RequestConfig={
    url:'user/list',
    method:HttpRequestMethod.POST,
    data:param,
    token:true,
  }
  return request(requestConfig);
}

// 将微信用户信息保存到数据库
export  function downloadAvatar(url:string): Promise<WechatMiniprogram.DownloadFileSuccessCallbackResult> {
  let requestConfig:RequestConfig={
    url:url,
    method:HttpRequestMethod.GET,
  }
  return  download(requestConfig)
}

/**
 * 
 * @param url 文件上传地址
 * @param filePath 文件地址
 * @param fileName 文件名称
 * @param formData 
 */
export  function uploadAvatar(filePath:string,
                              formData:any): Promise<LengyuHttpResponse> {
  let requestConfig:RequestConfig={
    url:"user/upload-file",
    method:HttpRequestMethod.GET,
    token:true,
    data:formData,
  }
  return  upload(requestConfig,filePath);
}